/**
 * 
 */
package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.ProductionLine;
import com.efast.dmom.aps.dao.RouteProdLineRelDao;
import com.efast.dmom.aps.entity.ProductionLineEntity;
import com.efast.dmom.aps.entity.RouteLineRelEntity;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * @author Aaron Liu
 *
 *         2020年2月13日
 */
@Component
public class RouteProdLineRelDataHandler {

	@Autowired
	private RouteProdLineRelDao routeProdLineDao;

	protected void saveRouteProdLineRel(long routeKey, String routeUuid, long defaultLineKey,
			List<ProductionLine> lineList) {

		if (routeKey <= 0 || lineList == null || lineList.size() <= 0) {
			return;
		}
		// table: aps_route_line_rel

		List<RouteLineRelEntity> routeLineRelList = new ArrayList<RouteLineRelEntity>();

		for (ProductionLine line : lineList) {

			ProductionLineEntity lineEntity = line.getInnerData();

			RouteLineRelEntity relEntity = new RouteLineRelEntity();

			relEntity.setSiteId(DataHandlerUtils.getSiteId());
			relEntity.setCreationTime(TimeUtil.getLocalDateTime());
			relEntity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
			relEntity.setCreator(SpringWebUtils.getRequestUser());
			relEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
			relEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			relEntity.setLastModifier(SpringWebUtils.getRequestUser());
			relEntity.setRouteUuid(routeUuid);
			relEntity.setRouteDefaultLine(line.getInnerData().isDefaultLine());
			// relEntity.setCreationPid(DataHandlerUtils.getDmomPid());
			// relEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
			relEntity.setRowStatus(1);
			relEntity.setRowTrxId(1);

			if (defaultLineKey == lineEntity.getId()) {
				relEntity.setRouteDefaultLine(true);
			}

			relEntity.setLineKey(lineEntity.getId());
			relEntity.setRouteKey(routeKey);

			routeLineRelList.add(relEntity);
		}
		// save
		routeProdLineDao.saveRouteProdLineRel(routeLineRelList);
	}

	public void removeRelByRouteId(long routeId) {

		routeProdLineDao.removeRelByRouteId(routeId);
	}

	public long getDefaultProdLineKey(long routeKey) {
		return routeProdLineDao.getDefaultProdLineKey(routeKey);
	}

}
